function onblur_grocery(field,text)
{
	if(document.getElementById(field).value == "")
	{
		document.getElementById(field).value = text;
		//document.getElementById(field).className = "input_grocery";
	}
}
function onfocus_grocery(field,text)
{

	if(document.getElementById(field).value == text)
	{
		document.getElementById(field).value = "";
		
	}
}
//on focus show grocery image to the right of add grocery button.
function show_grocery_image()
{

 document.getElementById("grocery_image").style.visibility = "";
}

//on blur hide grocery image
function hide_grocery_image()
{

 document.getElementById("grocery_image").style.visibility = "hidden";
}
//global variables for xmlhttp object.
var xmlHttpobj;
var id;
//validatins for adding and editing grocery
function validate_grocery(type,name,brand,quantity,date,schedule,user_id)
{
 
 var flag=1;
 if(document.getElementById(name).value=="")
 {
   alert("Enter Name");
   return false;
 }
 else if(document.getElementById(brand).value=="")
 {
   alert("Enter Brand");
  return false;
 }
 else if(document.getElementById(quantity).value=="")
 {
   alert("Enter Quantity");
   return false;
 }
 else if(document.getElementById(date).value=="")
 {
   alert("Enter Date");
  return false;
 }
 else if(document.getElementById(schedule).value=="")
 {
   alert("Select Schedule");
   return false;
 }
 if(type=='edit')
 {
 edit_grocery(user_id);
 }
 //if type is not edit then userid from add grocery item send it to sendgrocerydetails
 else
 {
  addgrocerydetails(type);
 }
   
}
// getting each and everything 
function addgrocerydetails(id)
{
document.getElementById('table_grocery').innerHTML="<img src="+loading_img_url+" style='padding-left:250px'/>";
	var name = document.getElementById("itemname").value;
	var brand = document.getElementById("itembrand").value;
	var quantity = document.getElementById("itemquantity").value;
	var date = document.getElementById("itemdate").value;
	var schedule = document.getElementById("schedule").value;
	var userid = id;
	var col_id = 'ss';
	var type = 'add';
	var loc = 'table_grocery';
	adddetails(name,brand,quantity,date,schedule,userid,type,loc,col_id,addgrocery_url);
}

//adding into database using ajax
function adddetails(name,brand,quantity,date,schedule,userid,type,loc,colid,url)
{
	loca = loc;
xmlHttpobj=GetxmlHttpobjObject();
if (xmlHttpobj==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
 
url=url+"?name="+name;
url=url+"&brand="+brand;
url=url+"&quantity="+quantity;
url=url+"&date="+date;
url=url+"&schedule="+schedule;
url=url+"&userid="+userid;
url=url+"&type="+type;
url=url+"&col_id="+colid;
url=url+"&sno="+loca;
url=url+"&sid="+Math.random();
xmlHttpobj.onreadystatechange=stateChanged_add;
xmlHttpobj.open("GET",url,true);
xmlHttpobj.send(null);
}

//after finishing adding in database display entire table from output of php file.
function stateChanged_add() 
{ 
if (xmlHttpobj.readyState==4)
 {   // alert(xmlHttpobj.responseText);
   try{
		  document.getElementById("table_grocery").innerHTML=xmlHttpobj.responseText;
	  	}
  catch(e)
  {
	  alert(e);
  } 
   
   
  }
}

function GetxmlHttpobjObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}  
  
//on clicking edit enable edit field and add particulat field values into edit tr.
function edit(id)
{
 
  document.getElementById("edit_tr").style.visibility="visible";
 document.getElementById("editid").value=document.getElementById("gid"+id).value;
 document.getElementById("editname").value=document.getElementById("name"+id).innerHTML;
 document.getElementById("editbrand").value=document.getElementById("brand"+id).innerHTML;
 document.getElementById("editquantity").value=document.getElementById("quantity"+id).innerHTML;
 document.getElementById("editdate").value=document.getElementById("date"+id).innerHTML;
 document.getElementById("editschedule").value=document.getElementById("schedule"+id).innerHTML;

}

//editing grocery in database.
function edit_grocery(user_id)
{
 id=document.getElementById("editid").value;

  document.getElementById('quantity'+id).innerHTML = "<img src="+loading_img_url_edit+" />";
  
  var name=document.getElementById("editname").value;
  var brand=document.getElementById("editbrand").value;
  var quantity=document.getElementById("editquantity").value;
  var date=document.getElementById("editdate").value;
  var schedule=document.getElementById("editschedule").value;
  
 
xmlHttpobj=GetxmlHttpobjObject();
if (xmlHttpobj==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url=editgrocery_url;
url=url+"?name="+name+"&brand="+brand+"&quantity="+quantity+"&date="+date+"&userid="+user_id+"&schedule="+schedule+"&id="+id+"&type=edit";
//alert(url);
xmlHttpobj.onreadystatechange=stateChangedobj;
xmlHttpobj.open("GET",url,true);
xmlHttpobj.send(null);
  
}
//after editing add tr using xml format response from php file.
function stateChangedobj() 
{ 
if (xmlHttpobj.readyState==4)
 {    
  
	    hidden_id = document.createElement('input');
        hidden_id.setAttribute('type','hidden');

	 	 // alert(xmlHttpobjobj.responseText);
	hidden_id.value  = xmlHttpobj.responseXML.getElementsByTagName('id')[0].childNodes[0].nodeValue;
	document.getElementById('name'+id).innerHTML = xmlHttpobj.responseXML.getElementsByTagName('name')[0].childNodes[0].nodeValue;
	document.getElementById('brand'+id).innerHTML = xmlHttpobj.responseXML.getElementsByTagName('brand')[0].childNodes[0].nodeValue;
	document.getElementById('date'+id).innerHTML = xmlHttpobj.responseXML.getElementsByTagName('date')[0].childNodes[0].nodeValue;
	document.getElementById('quantity'+id).innerHTML = xmlHttpobj.responseXML.getElementsByTagName('quantity')[0].childNodes[0].nodeValue;
	document.getElementById('schedule'+id).innerHTML = xmlHttpobj.responseXML.getElementsByTagName('schedule')[0].childNodes[0].nodeValue;
	
	 
		document.getElementById("edit_tr").style.visibility="hidden";
	document.getElementById('ajax_loader').innerHTML="";
   
  }
}
//deleting grocery item from database.
function delete_grocery(grocery_id)
{
 document.getElementById("edit_tr").style.visibility="hidden";
 alert("Are you sure want to delete this item?");
 //create on td with loading image
 id= grocery_id;
 td1=document.createElement('td');
 td1.setAttribute('colspan','5');
 td1.setAttribute('align','center');
 td1.innerHTML = "<img src="+loading_img_url_edit+" />";
 //remove all tds and append one td with loading image.
 tr=document.getElementById(id);
 while(tr.hasChildNodes()){
       tr.removeChild(tr.lastChild);
   
      }
 tr.appendChild(td1);
  xmlHttpobj=GetxmlHttpobjObject();
  if (xmlHttpobj==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }

  url=deletegrocery_url+"?id="+id+"&type=delete";
  xmlHttpobj.onreadystatechange=stateChanged_delete;
  xmlHttpobj.open("GET",url,true);
  xmlHttpobj.send(null);
}
function stateChanged_delete()
{
if (xmlHttpobj.readyState==4)
 {    
 //remove the tr after finishing deleting in database.
   tr=document.getElementById(id);
   tr.parentNode.removeChild(tr);
  
  }
}

